package com.ikkong.school.controller;

import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.school.model.School;
import com.ikkong.school.service.SchoolService;
import com.ikkong.school.service.impl.SchoolServiceImpl;
import com.ikkong.system.model.Dept;
import com.sun.deploy.cache.BaseLocalApplicationProperties;

/**
 * Generated by Blade.
 * 2018-07-09 22:30:16
 */
public class SchoolController extends UrlPermissController {
	private static String CODE = "school";
	private static String PERFIX = "school";
	private static String LIST_SOURCE = "School.list";
	private static String BASE_PATH = "/platform/school/";
	
	SchoolService service = new SchoolServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "school.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "school_add.html");
	}

	public void edit() {
		String id = getPara(0);
		School school = service.findById(id);
		setAttr("model", JsonKit.toJson(school));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "school_edit.html");
	}

	public void view() {
		String id = getPara(0);
		School school = service.findById(id);
		setAttr("model", JsonKit.toJson(school));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "school_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		School school = mapping(PERFIX, School.class);
        String name = school.getName();
        Dept dept = new Dept();
        dept.setPid(8);
        dept.setSimplename(name);
        dept.setFullname(name);
        dept.setNum(0);
        int id = Blade.create(Dept.class).saveRtId(dept);
        school.setD_id(id);
        boolean temp = service.save(school);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		School school = mapping(PERFIX, School.class);
		school.setVersion(school.getVersion()+1);
		boolean temp = service.update(school);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
